package com.tencent.wxcloudrun.controller;

import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Optional;

/**
 * @author xu
 * @date 2022/2/24 13:45
 */
@Controller
@RequestMapping("calligraphy")
public class CalligraphyController {

    private Logger logger = LoggerFactory.getLogger(CalligraphyController.class);

    @ResponseBody
    @GetMapping(value = "parseUrl")
    ApiResponse parseUrl(String url) {
        // http://127.0.0.1/calligraphy/parseUrl?url=https://gitlab.com/xuyq123/calligraphy/-/raw/master/README.md
        JSONObject object = new JSONObject();
        object.put("url", url);
        object.put("date", new Date());
        object.put("content", HttpUtil.sendGet(url, ""));
        return ApiResponse.ok(object);
    }

    /**
     * 主页页面
     *
     * @return API response html
     */
    @GetMapping
    public String index(Integer index) {
        Integer integer = Optional.ofNullable(index).orElse(1);
        if (integer == 1) {
            return "README";
        } else if (integer == 2) {
            return "无为徐生";
        } else if (integer == 3) {
            return "书法练习轨迹--明月几时有";
        }
        return "README";

    }


}
